<?php
/**
 *
 * ContentAction.class.php (前台内容模块)
 *
 */
class ContentAction extends PublicAction {

    public function index($catid='',$module='')
    {
        $this->Urlrule = F('Urlrule');

        if (empty($catid))
            $catid = intval($_REQUEST['id']);

        $p = I('get.p', 1);
        if ($catid) {
            $cat = $this->categorys[$catid];
            if (empty($module))
                $module = $cat['module'];

            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('catid',$catid);
        }

        $fields = F($this->mod[$module].'_Field');

        foreach ($fields as $key=>$r) {
            $fields[$key]['setup'] = string2array($fields[$key]['setup']);
        }

        $this->assign('fields', $fields);

        if($catid){
            $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
            $this->assign ('seo_title',$seo_title);
            $this->assign ('seo_keywords',$cat['keywords']);
            $this->assign ('seo_description',$cat['description']);

            //$where = " status=1 ";
            $where = " ";

            if ($cat['child']) {
                $where .= "  catid in(".$cat['arrchildid'].")";
            } else {
                $where .=  " catid=".$catid;
            }

            if (empty($cat['listtype'])) {

                $this->db = M($module);

                $count = $this->db->where($where)->count();

                if($count){
                    import( "@.ORG.Page" );
                    $listRows =  !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');
                    $page = new Page($count, $listRows);

                    $page->urlrule = geturl($cat,'',$this->Urlrule);
                    $pages = $page->wap_show();

                    $field =  $this->module[$this->mod[$module]]['listfields'];
                    $field =  $field ? $field : '*';
                    $list = $this->db->field($field)->where($where)->order('listorder desc,id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

                    $this->assign('pages',$pages);
                    $this->assign('list',$list);
                }

                $template_r = 'list';

            } else {
                $template_r = 'index';
            }
        } else {
            $template_r = 'list';
        }

        $template = $cat['wap_template_list'] ? $cat['wap_template_list'] : $template_r;

        $this->display($module.':'.$template);

    }

    public function detail($id='',$module='')
    {
        $this->Urlrule = F('Urlrule');
        $p = max(intval($_REQUEST[C('VAR_PAGE')]),1);
        $id = $id ? $id : intval($_REQUEST['id']);

        if($_GET['chid']) {
          $id = $_GET['chid'];
          $this->assign('chid',$_GET['chid']);
        }

        $module = $module ? $module : MODULE_NAME;
        $this->assign('module_name',$module);
        $this->db= M($module);;
        $data = $this->db->find($id);

        //关联信息
        if(!empty($data['gl'])){
          $gl_data = $this->db->field('url,title,thumb')->where('id in('.$data['gl'].')')->select();
        }

        $listorder = $data['listorder'];

        //上一个，下一个
        if($listorder!=0){
            $prea = $this->db->field('title,url')->where('catid="'.$data['catid'].'" AND '.$listorder.' > listorder')->order('listorder desc')->limit('1')->select();
            $next = $this->db->field('title,url')->where('catid="'.$data['catid'].'" AND '.$listorder.' < listorder')->order('listorder asc')->limit('1')->select();
        } else{
            $prea = $this->db->field('title,url')->where('catid="'.$data['catid'].'" AND '.$id.' < id')->order('id asc')->limit('1')->select();
            $next = $this->db->field('title,url')->where('catid="'.$data['catid'].'" AND '.$id.' > id')->order('id desc')->limit('1')->select();
        }

        $this->assign('prea',$prea[0]);
        $this->assign('next',$next[0]);


        $catid = $data['catid'];
        $cat = $this->categorys[$data['catid']];
        if(empty($cat['ishtml']))
          $this->db->where("id=".$id)->setInc('hits'); //添加点击次数

        $seo_title = $data['title'].'-'.$cat['catname'];
        $this->assign('seo_title',$seo_title);
        $this->assign('seo_keywords',$data['keywords']);
        $this->assign('seo_description',$data['description']);
        $this->assign('fields', F($cat['moduleid'].'_Field'));

        $fields = F($this->mod[$module].'_Field');

        foreach($data as $key=>$c_d){
            $setup='';
            $fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);
            if ($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text') {
                $data[$key.'_old_val'] =$data[$key];
                $data[$key]=fieldoption($fields[$key],$data[$key]);
            } elseif ($fields[$key]['type']=='images' || $fields[$key]['type']=='files') {
                if(!empty($data[$key])){
                    $p_data = explode(':::',$data[$key]);
                    $data[$key] = array();
                    foreach($p_data as $k=>$res){
                        $p_data_arr = explode('|',$res);
                        $data[$key][$k]['filepath'] = $p_data_arr[0];
                        $data[$key][$k]['filename'] = $p_data_arr[1];
                    }
                    unset($p_data);
                    unset($p_data_arr);
                }
            }
            unset($setup);
        }

        $this->assign('fields',$fields);

        //手动分页
        $CONTENT_POS = strpos($data['content'], '[page]');
        if($CONTENT_POS !== false) {

            $urlrule    = geturl($cat,$data,$this->Urlrule);
            $urlrule    = str_replace('%7B%24page%7D','{$page}',$urlrule);
            $contents   = array_filter(explode('[page]',$data['content']));
            $pagenumber = count($contents);

            for($i=1; $i<=$pagenumber; $i++) {
                $pageurls[$i] = str_replace('{$page}',$i,$urlrule);
            }

            $pages = content_pages($pagenumber,$p, $pageurls);
            //判断[page]出现的位置是否在文章开始
            if($CONTENT_POS<7) {
                $data['content'] = $contents[$p];
            } else {
                $data['content'] = $contents[$p-1];
            }

            $this->assign ('pages',$pages);
        }

        //判断模板文件
        if(!empty($data['template'])){
            $template = $data['template'];
        }elseif(!empty($cat['wap_template_show'])){
            $template = $cat['wap_template_show'];
        }else{
            $template = 'show';
        }

        $this->assign('catid',$catid);
        $this->assign($cat);
        $this->assign('gl_data',$gl_data);

        $this->assign ($data);
        $this->display($module.':'.$template);
    }

    public function hits()
    {
        $module    = $module ? $module : MODULE_NAME;
        $id        = $id ? $id : intval($_REQUEST['id']);
        $this->db = M($module);
        $this->db->where("id=".$id)->setInc('hits');

        if($module=='Download'){
            $r = $this->db->find($id);
            echo '$("#hits").html('.$r['hits'].');$("#downs").html('.$r['downs'].');';
        }else{
            $hits = $this->db->where("id=".$id)->getField('hits');
            echo '$("#hits").html('.$hits.');';
        }
        exit;
    }
}